package com.cmict.data.form.api.util;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmict.data.form.api.entity.dataobject.SysFormDO;
import com.cmict.data.form.api.service.SysCurrencyService;
import com.cmict.data.form.api.service.SysFormService;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.infra.api.rdb.RdbApi;
import com.cmict.data.system.api.dept.DeptApi;
import com.cmict.data.system.api.dept.PostApi;
import com.cmict.data.system.api.dept.dto.DeptRespDTO;
import com.cmict.data.system.api.dept.dto.PostDTO;
import com.cmict.data.system.api.dict.DictDataApi;
import com.cmict.data.system.api.dict.dto.DictDataDTO;
import com.cmict.data.system.api.user.AdminUserApi;
import com.cmict.data.system.api.user.dto.AdminUserRespDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.*;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

/**
 * @description: 导入字段类型工具类
 * @author: qiaoyu
 * @create: 2022-07-08 22:57
 **/
@Component
public class IsSelectUtil {

    @Autowired
    private SysFormService sysFormService;
    private static SysFormService realsysFormService;

    @Autowired
    private SysCurrencyService sysCurrencyService;
    private static SysCurrencyService realsysCurrencyService;

    @Autowired
    private DictDataApi dictDataService;
    private static DictDataApi realdictDataService;

    @Autowired
    private DeptApi deptService;
    private static DeptApi realdeptService;

    @Autowired
    private AdminUserApi adminUserService;
    private static AdminUserApi realadminUserService;

    @Autowired
    private PostApi postService;
    private static PostApi realpostService;

    @Autowired
    private RdbApi rdbApiService;
    private static RdbApi realrdbapiService;

    @PostConstruct
    public void init() {
        realsysFormService = sysFormService;
        realsysCurrencyService = sysCurrencyService;
        realdictDataService = dictDataService;
        realdeptService = deptService;
        realadminUserService = adminUserService;
        realpostService = postService;
        realrdbapiService = rdbApiService;
    }
    static Map<Object,Object> selectDataMapping(JSONObject field,String value, String datatype) {
        String req = "";
        boolean isselect = true;
        String returnValue = "";
        //静态
        Map<Object,Object> mapStatic = staticSelect(req,isselect,returnValue,field,value,datatype);
        req = (String) mapStatic.get("req");
        isselect = (boolean) mapStatic.get("isselect");
        returnValue = (String) mapStatic.get("returnValue");
        //数据字典
        Map<Object,Object> mapDictionary = dictionarySelect(req,isselect,returnValue,field,value,datatype);
        req = (String) mapDictionary.get("req");
        isselect = (boolean) mapDictionary.get("isselect");
        returnValue = (String) mapDictionary.get("returnValue");
        //数据源
        Map<Object,Object> mapDatasource = datasourceSelect(req,isselect,returnValue,field,value,datatype);
        req = (String) mapDatasource.get("req");
        isselect = (boolean) mapDatasource.get("isselect");
        returnValue = (String) mapDatasource.get("returnValue");
        //全部
        Map<Object,Object> map = new HashMap<>();
        map.put("req",req);
        map.put("isselect",isselect);
        map.put("value",returnValue);
        return map;
    }
    static Map<Object,Object> staticSelect(String req,Boolean isselect,String returnValue,JSONObject field,String value, String datatype) {
        //多选组件应用判断是否为多选组件
        JSONObject config = field.getJSONObject("__config__");
        String tagIcon = config.getString("tagIcon");
        //静态数据
        if("static".equals(datatype)){
            Boolean ret = Boolean.valueOf(field.getString("multiple"));
            if(ret || "checkbox".equals(tagIcon)){
                String msg = "";
                List<Object> objects = new ArrayList<>();
                JSONArray options = field.getJSONArray("options");
                for (int  i= 0; i <options.size() ; i++) {
                    objects.add(options.getJSONObject(i).getString("label"));
                    msg = msg + options.getJSONObject(i).getString("label")+",";
                }
                List<String> values = Arrays.asList(value.split(","));
                if(objects.containsAll(values) && ObjectUtil.isNotEmpty(value)){
                    if(ObjectUtil.isNotEmpty(values)){
                        for (int i = 0; i <values.size() ; i++) {
                            for (int  k= 0; k <options.size() ; k++) {
                                if(options.getJSONObject(k).getString("label").equals(values.get(i))){
                                    returnValue = returnValue + options.getJSONObject(k).getString("value")+",";
                                }
                            }
                        }
                    }
                    if(ObjectUtil.isNotEmpty(returnValue)){
                        if(returnValue.substring(returnValue.length()-1).equals(",")){
                            returnValue = returnValue.substring(0,returnValue.length() - 1);
                        }
                    }
                }else{
                    req = msg;
                    isselect = false;
                }
            }else{
                String msg = "";
                JSONArray options = field.getJSONArray("options");
                for (int  k= 0; k <options.size() ; k++) {
                    msg = msg + options.getJSONObject(k).getString("label")+",";
                    if(options.getJSONObject(k).getString("label").equals(value)){
                        returnValue = options.getJSONObject(k).getString("value");
                    }
                }
                List<String> values = Arrays.asList(value.split(","));
                if(values.size()>1){
                    req = "字段为单选;";
                    isselect = false;
                }
                if(msg.contains(",")){
                    List<String> stringList = Arrays.asList(msg.split(","));
                    if(!stringList.contains(value)){
                        req = msg;
                        isselect = false;
                    }
                }
            }
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("req",req);
        map.put("isselect",isselect);
        map.put("returnValue",returnValue);
        return map;
    }
    static Map<Object,Object> dictionarySelect(String req,Boolean isselect,String returnValue,JSONObject field,String value, String datatype) {
        //多选组件应用判断是否为多选组件
        JSONObject config = field.getJSONObject("__config__");
        String tagIcon = config.getString("tagIcon");
        //字典数据
        if("dictionary".equals(datatype)){
            Boolean ret = Boolean.valueOf(field.getString("multiple"));
            if(ret || "checkbox".equals(tagIcon)){
                String msg = "";
                List<Object> objects = new ArrayList<>();
                String dictType = field.getString("dictName");
                List<DictDataDTO> list = realdictDataService.getListByDictType(dictType).getCheckedData();
                for (int  i= 0; i <list.size() ; i++) {
                    objects.add(list.get(i).getLabel());
                    msg = msg + list.get(i).getLabel()+",";
                }
                List<String> values = Arrays.asList(value.split(","));
                if(objects.containsAll(values) && ObjectUtil.isNotEmpty(value)){
                    if(ObjectUtil.isNotEmpty(values)){
                        for (int i = 0; i <values.size() ; i++) {
                            for (int  k= 0; k <list.size() ; k++) {
                                if(list.get(k).getLabel().equals(values.get(i))){
                                    returnValue = returnValue + list.get(k).getValue()+",";
                                }
                            }
                        }
                    }
                    if(ObjectUtil.isNotEmpty(returnValue)){
                        if(returnValue.substring(returnValue.length()-1).equals(",")){
                            returnValue = returnValue.substring(0,returnValue.length() - 1);
                        }
                    }
                }else{
                    req = msg;
                    isselect = false;
                }
            }else{
                String msg = "";
                String dictType = field.getString("dictName");
                List<DictDataDTO> list = realdictDataService.getListByDictType(dictType).getCheckedData();
                for (int  k= 0; k <list.size() ; k++) {
                    msg = msg + list.get(k).getLabel()+",";
                    if(list.get(k).getLabel().equals(value)){
                        returnValue = list.get(k).getValue();
                    }
                }
                List<String> values = Arrays.asList(value.split(","));
                if(values.size()>1){
                    req = "字段为单选;";
                    isselect = false;
                }
                if(msg.contains(",")){
                    List<String> stringList = Arrays.asList(msg.split(","));
                    if(!stringList.contains(value)){
                        req = msg;
                        isselect = false;
                    }
                }
            }
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("req",req);
        map.put("isselect",isselect);
        map.put("returnValue",returnValue);
        return map;
    }
    static Map<Object,Object> datasourceSelect(String req,Boolean isselect,String returnValue,JSONObject field,String value, String datatype) {
        //多选组件应用判断是否为多选组件
        JSONObject config = field.getJSONObject("__config__");
        String tagIcon = config.getString("tagIcon");
        //数据源数据
        if("datasource".equals(datatype)){
            Boolean ret = Boolean.valueOf(field.getString("multiple"));
            if(ret || "checkbox".equals(tagIcon)){
                String msg = "";
                Long disTableName = field.getLong("tableName");
                String disFieldName = field.getString("fieldName");
                String dislinkName = field.getString("saveFieldName");
                if(ObjectUtil.isNotEmpty(disTableName) && ObjectUtil.isNotEmpty(disFieldName)){
                    //List<Map<String,Object>> sysDistanceDataVoList = realsysCurrencyService.getDistanceData(disTableName,disFieldName,dislinkName);
                    CommonResult json = realrdbapiService.sourceTestList(disTableName);
                    Map<Object,Object> mappp = (Map<Object, Object>) json.getData();
                    List<Map<Object,Object>>  sysDistanceDataVoList = (List<Map<Object, Object>>) mappp.get("rowMaps");
                    if(ObjectUtil.isNotEmpty(sysDistanceDataVoList)){
                        //List<Map<Object,Object>> returnMapListRecursion = (List<Map<Object, Object>>) selectDataMapping(disTableName,sysDistanceDataVoList).getData();
                        List<Object> objects = new ArrayList<>();
                        for (int k = 0; k <sysDistanceDataVoList.size() ; k++) {
                            msg = msg + sysDistanceDataVoList.get(k).get(disFieldName)+",";
                            objects.add(sysDistanceDataVoList.get(k).get(disFieldName));
                        }
                        List<String> values = Arrays.asList(value.split(","));
                        if(objects.containsAll(values) && ObjectUtil.isNotEmpty(value)){
                            if(ObjectUtil.isNotEmpty(values)){
                                for (int i = 0; i <values.size() ; i++) {
                                    for (int k = 0; k <sysDistanceDataVoList.size() ; k++) {
                                        if(sysDistanceDataVoList.get(k).get(disFieldName).equals(values.get(i))){
                                            returnValue = returnValue + String.valueOf(sysDistanceDataVoList.get(k).get(dislinkName))+",";
                                        }
                                    }
                                }
                            }
                            if(ObjectUtil.isNotEmpty(returnValue)){
                                if(returnValue.substring(returnValue.length()-1).equals(",")){
                                    returnValue = returnValue.substring(0,returnValue.length() - 1);
                                }
                            }
                        }else{
                            req = msg;
                            isselect = false;
                        }
                        /*if(!msg.contains(value)){
                            req = msg;
                            isselect = false;
                        }*/
                    }
                }
            }else{
                String msg = "";
                Long disTableName = field.getLong("tableName");
                String disFieldName = field.getString("fieldName");
                String dislinkName = field.getString("saveFieldName");
                CommonResult json = realrdbapiService.sourceTestList(disTableName);
                Map<Object,Object> mappp = (Map<Object, Object>) json.getData();
                List<Map<Object,Object>>  sysDistanceDataVoList = (List<Map<Object, Object>>) mappp.get("rowMaps");
                if(ObjectUtil.isNotEmpty(sysDistanceDataVoList)) {
                    //List<Map<Object,Object>> returnMapListRecursion = (List<Map<Object, Object>>) selectDataMapping(disTableName,sysDistanceDataVoList).getData();
                    List<Object> objects = new ArrayList<>();
                    for (int k = 0; k < sysDistanceDataVoList.size(); k++) {
                        msg = msg + sysDistanceDataVoList.get(k).get(disFieldName) + ",";
                        objects.add(sysDistanceDataVoList.get(k).get(disFieldName));
                    }
                    for (int k = 0; k <sysDistanceDataVoList.size() ; k++) {
                        if(sysDistanceDataVoList.get(k).get(disFieldName).equals(value)){
                            returnValue = String.valueOf(sysDistanceDataVoList.get(k).get(dislinkName));
                        }
                    }
                    List<String> values = Arrays.asList(value.split(","));
                    if(values.size()>1){
                        req = "字段为单选;";
                        isselect = false;
                    }
                    if(msg.contains(",")){
                        List<String> stringList = Arrays.asList(msg.split(","));
                        if(!stringList.contains(value)){
                            req = msg;
                            isselect = false;
                        }
                    }
                }
            }
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("req",req);
        map.put("isselect",isselect);
        map.put("returnValue",returnValue);
        return map;
    }
    /**
     *列表类型
     */
    @Transactional(rollbackFor = Exception.class)
    public static Boolean treeList(String json) {
        JSONObject personData = JSON.parseObject(json);
        JSONObject rightConfig = personData.getJSONObject("rightConfig");
        String panelType = rightConfig.getString("panelType");
        if(panelType.equals("tree-list")){ return false; }
        return true;
    }
}

